package com.whmnx.my.ui.viewmodel;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableList;

import com.whmnx.my.BR;
import com.whmnx.my.R;
import com.whmnx.my.databinding.FragmentFinancialBinding;
import com.whmnx.my.ui.adapter.ViewPagerCaiwuBindingAdapter;
import com.whmnx.my.ui.fragment.MyFinancialMangageFragment;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.binding.command.BindingConsumer;
import me.tatarka.bindingcollectionadapter2.BindingViewPagerAdapter;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

/**
 * author : huang
 * date   : 2020/9/30 0030 9:22
 * version: 1.0
 * @author Administrator
 */
public class FinancialModel extends BaseViewModel {

    MyFinancialMangageFragment mFragment;
    FragmentFinancialBinding mBinding;
    private ViewPagerCaiwuItemViewModel itemViewModel;

    public FinancialModel(@NonNull Application application) {
        super(application);
    }


    //给ViewPager添加ObservableList
    public ObservableList<ViewPagerCaiwuItemViewModel> items = new ObservableArrayList<>();

    //给ViewPager添加ItemBinding
    public ItemBinding<ViewPagerCaiwuItemViewModel> itemBinding = ItemBinding.of(BR.viewModel, R.layout.item_viewpager_cai);

    private String[] titles = new String[]{"待发货","待收货","待回复","已完成","退款/售后"};


    public void init(MyFinancialMangageFragment myFinancialMangageFragment, FragmentFinancialBinding binding) {
        this.mFragment = myFinancialMangageFragment;
        this.mBinding = binding;
    }


    public void addPage() {
        items.clear();
        //模拟3个ViewPager页面
        for (int i = 1; i <= titles.length; i++) {
            itemViewModel = new ViewPagerCaiwuItemViewModel(this, i);
            items.add(itemViewModel);
        }
    }


    //给ViewPager添加PageTitle
    public final BindingViewPagerAdapter.PageTitles<ViewPagerCaiwuItemViewModel> pageTitles = new BindingViewPagerAdapter.PageTitles<ViewPagerCaiwuItemViewModel>() {
        @Override
        public CharSequence getPageTitle(int position, ViewPagerCaiwuItemViewModel item) {
            return titles[position];
        }
    };
    //给ViewPager添加Adpter，请使用自定义的Adapter继承BindingViewPagerAdapter，重写onBindBinding方法
    public  ViewPagerCaiwuBindingAdapter adapter = new ViewPagerCaiwuBindingAdapter();
    //ViewPager切换监听
    public BindingCommand<Integer> onPageSelectedCommand = new BindingCommand<>(new BindingConsumer<Integer>() {
        @Override
        public void call(Integer index) {
            //ToastUtils.showShort("ViewPager切换：" + index);
              if (index == 1){
                    itemViewModel.index = 2;
                }else if (index == 2){
                    itemViewModel.index = 3;
                }else if (index == 3){
                    itemViewModel.index = 4;
                } else if (index == 4){
                    itemViewModel.index = 5;
                } else {
                    itemViewModel.index = 1;
                }
        }
    });
}
